package com.ysw.springbootproject.controller;

import com.ysw.springbootproject.common.Constants;
import com.ysw.springbootproject.common.Result;
import com.ysw.springbootproject.common.ResultGenerator;
import com.ysw.springbootproject.config.annotation.TokenToUser;
import com.ysw.springbootproject.entity.AdminUser;
import com.ysw.springbootproject.service.AdminUserService;
import com.ysw.springbootproject.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author 月上微
 * @date 2025/1/12
 * @since 17:06
 */
@RestController
@RequestMapping("/users")
public class AdminUserController {

    @Autowired
    private AdminUserService adminUserService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result list(@RequestParam Map<String, Object> params) {
        if (params.get("page") == null || ((String) params.get("page")).isBlank()
                || params.get("limit") == null || ((String) params.get("limit")).isBlank()) {
            return ResultGenerator.genErrorResult(Constants.RESULT_CODE_PARAM_ERROR, "参数异常!");
        }
        // 查询列表数据
        PageUtil pageUtil = new PageUtil(params);
        return ResultGenerator.genSuccessResult(adminUserService.getAdminUserPage(pageUtil));
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result login(@RequestBody AdminUser adminUser) {
        Result result = ResultGenerator.genFailResult("登录失败");
        if (adminUser.getUserName() == null || ((String) adminUser.getUserName()).isBlank()
                || adminUser.getPassword() == null || ((String) adminUser.getPassword()).isBlank()) {
            result.setMessage("请填写登录用户信息!");
        }
        System.out.println("adminUser: " + adminUser);
        AdminUser loginUser = adminUserService.updateTokenAndLogin(adminUser.getUserName(), adminUser.getPassword());
        System.out.println("loginUser: " + loginUser);
        if (loginUser != null) {
            result = ResultGenerator.genSuccessResult(loginUser);
        }
        return result;
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(@RequestBody AdminUser adminUser, @TokenToUser AdminUser loginUser) {
        if (loginUser == null) {
            return ResultGenerator.genErrorResult(Constants.RESULT_CODE_NOT_LOGIN, "未登录!");
        }
        if (adminUser.getUserName() == null || ((String) adminUser.getUserName()).isBlank()
                || adminUser.getPassword() == null || ((String) adminUser.getPassword()).isBlank()) {
            return ResultGenerator.genErrorResult(Constants.RESULT_CODE_PARAM_ERROR, "参数异常!");
        }
        AdminUser tempUser = adminUserService.selectByUserName(adminUser.getUserName());
        if (tempUser != null) {
            return ResultGenerator.genErrorResult(Constants.RESULT_CODE_PARAM_ERROR, "用户名已存在，请勿重复添加!");
        }
        if ("admin".endsWith(adminUser.getUserName().trim())) {
            return ResultGenerator.genErrorResult(Constants.RESULT_CODE_PARAM_ERROR, "不能添加admin用户！");
        }
        if (adminUserService.save(adminUser) > 0) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult("添加失败!");
        }
    }

    @RequestMapping(value = "/updatePassword", method = RequestMethod.PUT)
    public Result update(@RequestBody AdminUser adminUser, @TokenToUser AdminUser loginUser) {
        if (loginUser == null) {
            return ResultGenerator.genErrorResult(Constants.RESULT_CODE_NOT_LOGIN, "未登录!");
        }
        if (adminUser.getPassword() == null || ((String) adminUser.getPassword()).isBlank()) {
            return ResultGenerator.genErrorResult(Constants.RESULT_CODE_PARAM_ERROR, "请输入密码!");
        }
        AdminUser tempUser = adminUserService.selectById(loginUser.getId());
        if (tempUser != null) {
            return ResultGenerator.genErrorResult(Constants.RESULT_CODE_PARAM_ERROR, "用户不存在!");
        }
        if ("admin".endsWith(tempUser.getUserName().trim())) {
            return ResultGenerator.genErrorResult(Constants.RESULT_CODE_PARAM_ERROR, "不能修改admin用户！");
        }
        tempUser.setPassword(adminUser.getPassword());
        if (adminUserService.updatePassword(adminUser) > 0) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult("修改失败");
        }
    }

    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Result delete(@RequestBody Integer[] ids, @TokenToUser AdminUser loginUser) {
        if (loginUser == null) {
            return ResultGenerator.genErrorResult(Constants.RESULT_CODE_NOT_LOGIN, "未登录!");
        }
        if (ids.length < 1) {
            return ResultGenerator.genErrorResult(Constants.RESULT_CODE_PARAM_ERROR, "参数异常!");
        }
        if (adminUserService.deleteBatch(ids) > 0) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult("删除失败");
        }
    }
}
